package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IPictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PictureServiceImpl implements IPictureService {

    @Autowired
    private PictureMapper pictureMapper;

    public PictureServiceImpl() {
        log.debug("创建业务对象: PictureServiceImpl");
    }

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理添加图片业务,参数: {}", pictureAddNewDTO);
        //从参数对象中获取相册id
        Long albumId = pictureAddNewDTO.getAlbumId();
        //检查相册是否存在（图片表中是否已经存相册）
        log.debug("检查相册是否存在");
        int count = pictureMapper.countByAlbumId(albumId);
        if(count>0){
            log.debug("相册存在,将向图片表中插入数据");
            Picture picture = new Picture();
            //album.getName().toLowerCase();
            BeanUtils.copyProperties(pictureAddNewDTO,picture);
            log.debug("即将插入图片数据:{}",picture);
            int rows = pictureMapper.insert(picture);
            if (rows != 1) {
                String message = "添加图片失败，服务器忙，请稍后再尝试！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_INSERT, message);
            }
            log.debug("插入图片数据完成");
        }

        //否: 相册名称没有被占用, 则向相册表中插入数据

    }
}




